
package game.kill.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

//常用的工具方法
public class CommonUtil {

	private static Logger logger = Logger.getLogger(CommonUtil.class);

	public static String hiddenName(String name) {
		if (StringUtils.isBlank(name) || name.length() <= 4) {
			return name;
		}
		String header = name.substring(0, 2);
		String end = name.substring(name.length() - 2, name.length());
		String hiddenName = header + "****" + end;
		return hiddenName;
	}

	// 修改nameList,返回同一个对象的引用
	public static List<String> hiddenName(List<String> nameList) {
		if (nameList == null || nameList.size() == 0) {
			return nameList;
		}
		int size = nameList.size();
		for (int index = 0; index < size; index++) {
			String name = nameList.get(index);
			String hiddenName = hiddenName(name);
			nameList.set(index, hiddenName);
		}
		return nameList;
	}

	
	


	public static String list2String(List<?> list, String separator) {

		StringBuilder builder = new StringBuilder();

		int size = list.size();
		for (int index = 0; index < size; index++) {
			if (index == 0) {
				Object str = list.get(index);
				builder.append(str);
			} else {
				Object str = separator + list.get(index);
				builder.append(str);
			}
		}
		String finalStr = builder.toString();
		return finalStr;

	}

	

	
	
	public static boolean isAjaxRequest(HttpServletRequest request){
		String requestedWith=request.getHeader("x-requested-with");  
		if(StringUtils.isEmpty(requestedWith)){
			return false;
		}else if(StringUtils.isNotEmpty(requestedWith) && requestedWith.equals("XMLHttpRequest")){
			return true;
		}
		return false;
	}

	public static String getRemoteIp(HttpServletRequest request) {
		String remoteIp = request.getHeader("x-forwarded-for");
		if (remoteIp == null || remoteIp.isEmpty()
				|| "unknown".equalsIgnoreCase(remoteIp)) {
			remoteIp = request.getHeader("X-Real-IP");
		}
		if (remoteIp == null || remoteIp.isEmpty()
				|| "unknown".equalsIgnoreCase(remoteIp)) {
			remoteIp = request.getHeader("Proxy-Client-IP");
		}
		if (remoteIp == null || remoteIp.isEmpty()
				|| "unknown".equalsIgnoreCase(remoteIp)) {
			remoteIp = request.getHeader("WL-Proxy-Client-IP");
		}
		if (remoteIp == null || remoteIp.isEmpty()
				|| "unknown".equalsIgnoreCase(remoteIp)) {
			remoteIp = request.getHeader("HTTP_CLIENT_IP");
		}
		if (remoteIp == null || remoteIp.isEmpty()
				|| "unknown".equalsIgnoreCase(remoteIp)) {
			remoteIp = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (remoteIp == null || remoteIp.isEmpty()
				|| "unknown".equalsIgnoreCase(remoteIp)) {
			remoteIp = request.getRemoteAddr();
		}
		if (remoteIp == null || remoteIp.isEmpty()
				|| "unknown".equalsIgnoreCase(remoteIp)) {
			remoteIp = request.getRemoteHost();
		}
		if (remoteIp != null && remoteIp.indexOf(",") != -1) {
			remoteIp = remoteIp.substring(remoteIp.lastIndexOf(",") + 1,
					remoteIp.length()).trim();
		}
		return remoteIp;
	}
	
	public static void returnMessage(HttpServletResponse response, String content) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		PrintWriter pWriter = null;
		try {
			pWriter = response.getWriter();
			pWriter.write(content);
		} catch (IOException e) {
			logger .error("returnMessage", e);
		} finally {
			if (pWriter != null) {
				pWriter.flush();
				pWriter.close();
			}
		}
	}

	
}
